package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.graph.Graph;
import io.minio.messages.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private  FeeValueService feeValueService;

    @Autowired
    private  LabelInfoService labelInfoService;

    @Autowired
    private  FacilityInfoService facilityInfoService ;

    @Autowired
    private  RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;


    //保存或更新
    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {

        boolean isUpdate = apartmentSubmitVo.getId() != null;
        //1.保存或者更新 apartment 对象
        saveOrUpdate(apartmentSubmitVo);//mybatis-plus自动主键回显;
        //2. 判断id是否存在 （删除相关联的数据  图片 ， 标签中间表，配套中间表 ， 杂费中间表）
        if (isUpdate) {
            //根据itemId 和 itemType
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);


            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        }

        //1.插入图片列表

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {

                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphInfo.getUrl());
                graphInfoArrayList.add(graphInfo);

            }
            graphInfoService.saveBatch(graphInfoArrayList);
        }

        //2.插入配套列表
        List<Long> facilityInfoIdList  = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            ArrayList<ApartmentFacility> facilities = new ArrayList<>();
            for (Long facilityId:facilityInfoIdList) {
                ApartmentFacility facility =
                        ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).build();
                facilities.add(facility);
            }
            apartmentFacilityService.saveBatch(facilities);
        }

        //3.插入标签列表
        List<Long> lableIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(lableIds)){
            ArrayList<ApartmentLabel> labelArrayList = new ArrayList<>();
            for (Long lableId:lableIds) {
                ApartmentLabel apartmentLabel
                        = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).build();
                labelArrayList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labelArrayList);
        }

        //4。插入杂费列表
        List<Long> feeValues = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValues)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValues) {
                ApartmentFeeValue apartmentFeeValue =
                        ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);

        }

    }

    //根据id查询公寓信息
    @Override
    public ApartmentDetailVo findApartmentDetailVo(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = getById(id);
        if (apartmentInfo==null){
            return null;
        }
        //查询图片信息
        List<GraphVo> graphVoList =
                graphInfoService.queryGraphVolist(id,ItemType.APARTMENT);
        //查询标签信息
        List<LabelInfo> labelInfos = labelInfoService.queryLableinfos(id);
        //查询配套信息
        List<FacilityInfo> facilityInfos = facilityInfoService.queryFacilityInfo(id);
        //查询杂费信息
        List<FeeValueVo> feeValueVoList = feeValueService.queryFeeValueVoList(id);
        //装到vo返回即可
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFacilityInfoList(facilityInfos);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;

    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long aLong = roomInfoMapper.selectCount(lambdaQueryWrapper);
        if (aLong>0) {
            throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //1.删除照片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //2.删除标签信息
        LambdaQueryWrapper<ApartmentLabel> labelInfoLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        labelInfoLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(labelInfoLambdaQueryWrapper);

        //3.删除配套信息
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //4.删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //5.删除ApartmentInfo
        super.removeById(id);
    }

    //根据条件分页查询公寓列表
    @Override
    public IPage<ApartmentItemVo> customApartmentPage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.customApartmentPage(page, queryVo);
    }
}




